package yy.com.yjboview.viewutil;

import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

/**
 * @description: <描述当前版本功能>
 * <p>
 * 简单的继承view类--基础知识
 * 引用：http://www.idtkm.com/customview/customview1/
 * </p>
 * @author: yjbo
 * @date: 2016-08-03 11:09
 */
public class SimpleViewUtil3 extends View {

    //    static Canvas mcanvas;
    //创建画笔
    static Paint mPaint = new Paint();
    //宽高
    private static int mWidth;
    private static int mHeight;
    private static int status = -1;

    public SimpleViewUtil3(Context context, int i) {
        super(context);
        status = i;
    }

    public SimpleViewUtil3(Context context) {
        super(context);
        initPain();
    }

    public SimpleViewUtil3(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPain();
    }

    public SimpleViewUtil3(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPain();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)//21版本
    public SimpleViewUtil3(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initPain();
    }

    private void initPain() {
        initAnimator(animatorDuration);
        mPaint.setStyle(Paint.Style.FILL);//设置画笔类型
        mPaint.setAntiAlias(true);//抗锯齿
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    //布局1
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.d("yjbo-onLayout", "getLeft==" + getLeft());
    }

    //布局1
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    //绘制
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        mcanvas = canvas;
        canvas.translate(mWidth / 2, mHeight / 2);// 将画布坐标原点移动到中心位置

        //x,y轴
        paintXY(canvas);
//        //绘制坐标原点
//        mPaint.setStyle(Paint.Style.STROKE);
//        mPaint.setColor(Color.GREEN);//设置画笔颜色
//        mPaint.setStrokeWidth(10);//为了看得清楚,设置了较大的画笔宽度
//        float point = Math.min(mWidth,mHeight)*0.2f/2;
//        float r = point*(float) Math.sqrt(2);
//        canvas.drawPoints(new float[]{-point, -point, point, -point}, mPaint);
//        RectF rectF = new RectF(-r,-r,r,r);
//        //扇形
//        canvas.drawArc(rectF, 0, 180, false, mPaint);
        doubanAnimator(canvas, mPaint);

        //如果不在Canvas里面添加invalidate();那么下次调用就不会更新，
        invalidate();
    }

    //绘制矩形
    private void paintRect(Canvas canvas) {
        mPaint.setStyle(Paint.Style.STROKE);//设置画笔类型
        mPaint.setStrokeWidth(3);
        canvas.drawRect(-mWidth / 8, -mHeight / 8, mWidth / 8, mHeight / 8, mPaint);
    }

    //x,y轴
    private void paintXY(Canvas canvas) {
        mPaint.setColor(Color.BLACK);//设置画笔颜色
        mPaint.setStrokeWidth(3);
        //绘制X轴箭头
        canvas.drawLines(new float[]{
                mWidth / 2 * 0.8f, 0, mWidth / 2 * 0.8f * 0.95f, -mWidth / 2 * 0.8f * 0.05f,
                mWidth / 2 * 0.8f, 0, mWidth / 2 * 0.8f * 0.95f, mWidth / 2 * 0.8f * 0.05f,
                -mWidth / 2 * 0.8f, 0, mWidth / 2 * 0.8f, 0
        }, mPaint);
        //绘制Y轴箭头
        canvas.drawLines(new float[]{
                -mWidth / 2 * 0.8f * 0.05f, -mHeight / 2 * 0.8f * 0.95f, 0, -mHeight / 2 * 0.8f,
                mWidth / 2 * 0.8f * 0.05f, -mHeight / 2 * 0.8f * 0.95f, 0, -mHeight / 2 * 0.8f,
                0, mHeight / 2 * 0.8f, 0, -mHeight / 2 * 0.8f
        }, mPaint);
    }

    //事件处理
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }


    public static void changeDraw(int i) {
        status = i;
    }


    //动画
    private ValueAnimator animator;
    private float animatedValue;
    private long animatorDuration = 50000;//动画执行的总时间
    private TimeInterpolator timeInterpolator = new DecelerateInterpolator();

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void initAnimator(long duration) {
        if (animator != null && animator.isRunning()) {
            animator.cancel();
            animator.start();
        } else {
            animator = ValueAnimator.ofFloat(0, 855).setDuration(duration);
            animator.setInterpolator(timeInterpolator);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    animatedValue = (float) animation.getAnimatedValue();//最大就是855
                    invalidate();
                }
            });
            animator.start();
        }
    }

    /***
     * 得分析
     * @param canvas
     * @param mPaint
     */
    private void doubanAnimator(Canvas canvas, Paint mPaint) {
        mPaint.setStyle(Paint.Style.STROKE);//描边
        mPaint.setStrokeCap(Paint.Cap.ROUND);//圆角笔触
        mPaint.setColor(Color.GREEN);
        mPaint.setStrokeWidth(15);
        float point = Math.min(mWidth, mHeight) * 0.1f / 2;
        float r = point * (float) Math.sqrt(2);
        RectF rectF = new RectF(-r, -r, r, r);
        canvas.save();
        Log.d("yjbo-doubanAnimator", "animatedValue==" + animatedValue);
        // rotate
        if (animatedValue >= 135) {//到这个值得时候才开始转动
            canvas.rotate(animatedValue - 135);//855-135=720,相当于没动
        }

        // draw mouth
        float startAngle = 0, sweepAngle = 0;
        if (animatedValue < 135) {
            startAngle = animatedValue + 5;
            sweepAngle = 170 + animatedValue / 3;
        } else if (animatedValue < 270) {
            startAngle = 135 + 5;
            sweepAngle = 170 + animatedValue / 3;
        } else if (animatedValue < 630) {
            startAngle = 135 + 5;
            sweepAngle = 260 - (animatedValue - 270) / 5;
        } else if (animatedValue < 720) {
            startAngle = 135 - (animatedValue - 630) / 2 + 5;
            sweepAngle = 260 - (animatedValue - 270) / 5;
        } else {
            startAngle = 135 - (animatedValue - 630) / 2 - (animatedValue - 720) / 6 + 5;
            sweepAngle = 170;
        }
        canvas.drawArc(rectF, startAngle, sweepAngle, false, mPaint);

        mPaint.setColor(Color.RED);
        // draw eye
        canvas.drawPoints(new float[]{
                -point, -point
                , point, -point
        }, mPaint);

        canvas.restore();
    }
}
